//
//  MCPublicTableViewModel.swift
//  UITableViewDemo
//
//  Created by daqsoft-mac on 2022/5/11.
//

import UIKit

class MCPublicTableViewSectionModel: NSObject {
    
    /// 第几个section,可不连续，但能被替换
    var sort : NSInteger = 0
    
    /// section中Cell的个数
    var cellNum: Int {
        get {
            return cellArray.count
        }
    }
    
    /// 懒加载section的数据模型数组
    lazy var cellArray : Array<MCPublicTableViewCellProtocol?> = {
        return Array()
    }()
    
    /// section的Header协议模型
    var sectionHeaderProtocol : MCPublicTableViewSectionHeaderFooterViewModel?
    
    /// section的Footer协议模型
    var sectionFooterProtocal : MCPublicTableViewSectionHeaderFooterViewModel?
    
    
    /// 重写初始化方法，初始化头尾模型
    override init() {
        let header : MCPublicTableViewSectionHeaderFooterViewModel = MCPublicTableViewSectionHeaderFooterViewModel()
        header.height = 0
        header.identifier = ""
        
        let footer : MCPublicTableViewSectionHeaderFooterViewModel = MCPublicTableViewSectionHeaderFooterViewModel()
        header.height = 0
        header.identifier = ""
        
        self.sectionHeaderProtocol = header
        self.sectionFooterProtocal = footer
    }
    
    init(headerIdentifier : String? = "", headerHeight : CGFloat? = 0, footerIdentifier : String? = "", footerHeight : CGFloat? = 0, sort : NSInteger? = -1 ) {
        self.sort = sort!
        let header : MCPublicTableViewSectionHeaderFooterViewModel = MCPublicTableViewSectionHeaderFooterViewModel()
        header.height = headerHeight!
        header.identifier = headerIdentifier!
        
        let footer : MCPublicTableViewSectionHeaderFooterViewModel = MCPublicTableViewSectionHeaderFooterViewModel()
        footer.height = footerHeight!
        footer.identifier = footerIdentifier!
        
        self.sectionHeaderProtocol = header
        self.sectionFooterProtocal = footer
    }
}

extension MCPublicTableViewSectionModel {
    
    /// 添加CellModel方法
    /// - Parameter model: 准寻CellProtocol的CellModel
    func addSectionCellModel(_ model : MCPublicTableViewCellProtocol?) {
        guard let model = model else {
            return
        }
        let cellModel = model as! MCPublicTableViewCellModel
        if cellModel.sorting == -1 {
            cellArray.append(cellModel)
        }
        var array = Array<MCPublicTableViewCellModel>()
        for cellProtocol in cellArray {
            guard let cellProtocol = cellProtocol else {
                return
            }
            let cellProtocolModel = cellProtocol as! MCPublicTableViewCellModel
            if cellProtocolModel.sorting != cellModel.sorting {
                array.append(cellProtocolModel)
            }
        }
        array.append(cellModel)
        array.sort()
        cellArray = array
    }
}

extension MCPublicTableViewSectionModel : Comparable {
    static func < (lhs: MCPublicTableViewSectionModel, rhs: MCPublicTableViewSectionModel) -> Bool {
        return lhs.sort < rhs.sort
    }
    
    
}
